package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuAttrValueService;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.ValuesSkuJsonVo;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author tangsy
 * @Description TODO
 * @date 2022/12/4 12:15
 * @Version 1.0
 */
@RequestMapping("/api/inner/rpc/product")
@RestController
public class SkuRpcController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @GetMapping("skuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    @GetMapping("/skuimages/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId") Long skuId){
        List<SkuImage> skuImages = skuImageService.lambdaQuery()
                .eq(SkuImage::getSkuId,skuId)
                .list();
        return Result.ok(skuImages);
    }

    @GetMapping("/skusaleattr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable("spuId") Long spuId,
                                                    @PathVariable("skuId") Long skuId){

        List<SpuSaleAttr>  saleAttrs = spuSaleAttrService.getSpuSaleAttrListOrder(spuId,skuId);

        return Result.ok(saleAttrs);
    }

    @GetMapping("/valuesSkuJson/{spuId}")
    public Result<String> getValuesSkuJson(@PathVariable("spuId") Long spuId){
        String json = spuSaleAttrService.getValuesSkuJson(spuId);
        return Result.ok(json);
    }

}
